using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Xml;
using System.Windows.Forms;

namespace PipeAuto.MobileUti
{
    /// <summary>
    /// configuration file utility
    /// </summary>
    public class Configuration
    {

        #region DataField

        private static Hashtable appSettings = null;
        private const string appSettingTitle = "appSettings";

        /// <summary>
        /// execute file path
        /// </summary>
        public static string AppFilePath = Assembly.GetCallingAssembly().GetName().CodeBase;


        #endregion



        #region Properties


        /// <summary>
        /// application's settings
        /// </summary>
        public static Hashtable AppSettings
        {
            get
            {
                if (Configuration.appSettings == null)
                {
                    Configuration.Refresh();
                }
                return Configuration.appSettings;
            }
        }


        #endregion

        public static void Refresh()
        {
            //clear app setting
            if (Configuration.appSettings != null)
            {
                Configuration.appSettings.Clear();
            }
            else
            {
                Configuration.appSettings = new Hashtable();
            }


            //get xml document
            XmlDocument myDoc = new XmlDocument();
            myDoc.Load(AppFilePath + ".config");

            XmlNodeList myNodeList = myDoc.GetElementsByTagName(appSettingTitle);

            //insert into hashtable
            foreach (XmlNode myNode in myNodeList)
            {
                foreach (XmlNode myEleNode in myNode.ChildNodes)
                {
                    appSettings.Add(myEleNode.Attributes["key"].Value, myEleNode.Attributes["value"].Value);
                }
            }
        }

        public static void Save()
        {
            if (Configuration.appSettings == null)
            {
                return;
            }


            XmlTextWriter myWriter = new XmlTextWriter(Configuration.AppFilePath + ".config", System.Text.Encoding.UTF8);

            myWriter.WriteStartDocument();
            myWriter.WriteStartElement("configuration");
            myWriter.WriteStartElement("appSettings");

            foreach (DictionaryEntry myEle in Configuration.appSettings)
            {
                myWriter.WriteStartElement("add");


                //key attribute
                myWriter.WriteStartAttribute("key", string.Empty);
                myWriter.WriteRaw(myEle.Key.ToString());
                myWriter.WriteEndAttribute();

                //value attribute
                myWriter.WriteStartAttribute("value", string.Empty);
                myWriter.WriteRaw(myEle.Value.ToString());
                myWriter.WriteEndAttribute();

                myWriter.WriteEndElement();
            }

            myWriter.WriteEndElement();
            myWriter.WriteEndElement();
            myWriter.WriteEndDocument();
            myWriter.Close();
            
        }
    }
}
